//
//  LXAPMKitEntryView.m
//  APMDemo
//
//  Created by Jack on 2020/7/17.
//  Copyright © 2020 LX. All rights reserved.
//

#import "LXAPMKitEntryView.h"
#import "LXAPMKitDefine.h"

#import "LXAPMKitHomeWindow.h"

const float kEntryViewSize = 50;


@interface LXAPMKitEntryView ()
/** 工具按钮 */
@property (nonatomic, strong) UIButton *toolButton;

@end

@implementation LXAPMKitEntryView

- (id)initWithStartPoint:(CGPoint)point{
    
    CGFloat x = point.x;
    if (x < 0.0) {
        x = 0.0;
    }
    else if (x > LXAPMKitScreenWidth - kEntryViewSize){
        x = LXAPMKitScreenWidth - kEntryViewSize;
    }
    
    CGFloat y = point.y;
    if (y < 0.0) {
        y = 0.0;
    }
    else if (y > LXAPMKitScreenHeight - kEntryViewSize){
        y = LXAPMKitScreenHeight - kEntryViewSize;
    }
    
    self = [super initWithFrame:CGRectMake(x, y, kEntryViewSize, kEntryViewSize)];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 界面设置
- (void)setupUI{
    
    self.backgroundColor     = [UIColor clearColor];
    self.windowLevel         = UIWindowLevelStatusBar + 100;
    self.layer.masksToBounds = YES;
    self.rootViewController  = [[UIViewController alloc] init];
    //工具按钮
    self.toolButton.center = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
    [self.rootViewController.view addSubview:self.toolButton];
    [self makeKeyAndVisible];
    
    //手势
    UIPanGestureRecognizer *pan = nil;
    pan = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(panGesture:)];
    [self addGestureRecognizer:pan];
    
}

#pragma mark - 手势
- (void)panGesture:(UIPanGestureRecognizer *)sender{
    
    //1、获得拖动位移
    CGPoint offsetPoint = [sender translationInView:sender.view];
    //2、清空拖动位移
    [sender setTranslation:CGPointZero inView:sender.view];
    //3、重新设置控件位置
    UIView *panView = sender.view;
    __block CGFloat newX = panView.center.x+offsetPoint.x;
    __block CGFloat newY = panView.center.y+offsetPoint.y;
    if (newX < kEntryViewSize/2) {
       newX = kEntryViewSize/2;
    }
    if (newX > LXAPMKitScreenWidth - kEntryViewSize/2) {
       newX = LXAPMKitScreenWidth - kEntryViewSize/2;
    }
    if (newY < kEntryViewSize/2) {
       newY = kEntryViewSize/2;
    }
    if (newY > LXAPMKitScreenHeight - kEntryViewSize/2) {
       newY = LXAPMKitScreenHeight - kEntryViewSize/2;
    }
    
    panView.center = CGPointMake(newX, newY);
    if (sender.state == UIGestureRecognizerStateCancelled ||
        sender.state == UIGestureRecognizerStateEnded ||
        sender.state == UIGestureRecognizerStateFailed) {
        
        [UIView animateWithDuration:0.3
                         animations:^{
            
            if (newX != kEntryViewSize/2.0 ||
                newX != LXAPMKitScreenWidth-kEntryViewSize/2.0) {
                
                if (newX > LXAPMKitScreenWidth/2.0) {
                    newX = LXAPMKitScreenWidth-kEntryViewSize/2.0;
                }
                else{
                    newX = kEntryViewSize/2.0;
                }
                panView.center = CGPointMake(newX, newY);
            }
        }];
        
        
    }
    
}

#pragma mark - 工具按钮点击
- (void)toolButtonClicked:(UIButton *)button{
    
    [LXAPMKitHomeWindow shareInstance].hidden = ![LXAPMKitHomeWindow shareInstance].hidden;
    
}

#pragma mark - Getter
- (UIButton *)toolButton{
    
    if(!_toolButton){
        
        UIImage *btnImage = [UIImage imageNamed:@"LXResource.bundle/lxapmkit_icon"];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame     = CGRectMake(0, 0, 30, 30);
        button.backgroundColor = [UIColor clearColor];
        [button setImage:btnImage forState:UIControlStateNormal];
        [button addTarget:self action:@selector(toolButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        _toolButton = button;
    }
    
    return _toolButton;
}

@end
